<?php
 include "counter2.php";
// $self_url = "http://" . $HTTP_HOST . $REQUEST_URI;
// $limit = $counter->get_input('limit', 'number', 'get');
 $key = $counter->get_input('key', '', 'get');
 $name = $counter->get_input('name', '', 'get');
// if (!$limit) {
//     $limit = 20;
// } 

pagestart(); ?>
<html>
<?php pagehead(_("Search for some kinds of machine information")) ?>
<BODY bgcolor="white">
<?php pagetop(_("Search for some kinds of machine information")) ?>
<hr>
<h2><?php print _("Search for machines belonging to a registration") ?></h2>
<form action="<?php echo $self_url ?>">
<table>
<tr><td><?php print _("User key or email address:") ?></td>
<td> <input name="key" value="<?php echo stripslashes($key) ?>"></td></tr>
</table>
<?php print "<input type=\"submit\" value=\"" . _("Search") . "\">" ?>
<?php print "<input type=\"reset\" value=\"" . _("Clear") . "\">" ?>
</form>

<?php
if ($key) {
    /*
    ** Validate if $key is a regular email address. 
    */
    if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $key)) {
       // print "<br>step 1<br>";
       $record = mysql_query("select owner from email where email.f_key = '" . $key . "'");
       if (!$record) {
          print mysql_error();
          die("Query 1 failed");
       }
       $arr = mysql_fetch_array($record);
       $realkey = $arr[owner];
       } else {
          /*
          ** If the string only contains numbers, it can be a valid key
          */
          if(ereg("^[0-9]+$",$key)) {
             // print "<br>step 2<br>";
             $realkey = $key;
          } else {
             /*
             ** Otherwise the string contents are crap
             */ 
             // print "<br>step 3<br>";
             $realkey = "";
          }
       } 

    if ($realkey) {
       $query = mysql_query("SELECT machines.f_key, automach.uniqueid, machines.name, unix_timestamp(automach.updated) as updatetime, automach.uptime FROM machines LEFT JOIN automach ON machines.f_key = automach.f_key WHERE machines.owner = '" . $realkey . "' ORDER BY f_key");
       if (!$query) {
          print mysql_error();
          die("Query failed");
       }

print "<table border>";
print "<tr><th>" . _("ID") . "</th><th>" . _("Name") . "</th><th>" . _("Unique auto-id") . "</th><th>" . _("Last auto-update") . "</th><th>" . _("Uptime") . "</th></tr>";

   while ($arr = mysql_fetch_array($query)) {
        # Try to suppress irritation from automach duplicates
        if ($prevkey <> $arr[f_key]) {
            print "<tr><td>" . $arr[f_key];
            print "<td><a href=\"$counter_site/cgi-bin/runscript/machine-show.cgi?machine=$arr[f_key]";
            maybecookie("&");
            print "\">";
            if ($arr[name]) {
               print $arr[name];
            } else {
               print _("Nameless");
            }
            print "</a>";
            $prevkey = $arr[f_key];
        } else {
           print "<tr><td><td>";
       }
        print "<td>";
        if ($arr[uniqueid]) {
           print "$arr[uniqueid]";
        }
        print "<td>";
        if ($arr[updatetime]) {
            print strftime("%Y-%m-%d %H:%M:%S", $arr[updatetime]);
        } else {
            print "<a href=\"$counter_site/scripts/\">No auto</a>";
        }
        if ($arr[uptime]) {
            printf("<td>%.1f", $arr[uptime]);
        }
        print "\n";
       } 
       } else {
         print "<font color=red><b>" . _("Email address does not exist in database or invalid email address or key entered as search string") . "</b></font>";
       }
    }
?>
</table>

<hr>
<h2><?php print _("Search machines by name") ?></h2>

<form action="<?php echo $self_url ?>">
<table>
<tr><td><?php print _("Machine hostname of domainname:") ?></td>
<td> <input name="name" value="<?php echo $name ?>"></td></tr>
</table>
<?php print "<input type=\"submit\" value=\"" . _("Search") . "\">" ?>
<?php print "<input type=\"reset\" value=\"" . _("Clear") . "\">" ?>
</form>

<?php
if ($name) {
    $query = mysql_query("SELECT machines.f_key, automach.uniqueid, machines.name, unix_timestamp(automach.updated) as updatetime, automach.uptime, machines.owner FROM machines LEFT JOIN automach ON machines.f_key = automach.f_key WHERE machines.name LIKE '%$name%' ORDER BY f_key");
    if (!$query) {
        print mysql_error();
        die("Query failed");
    }
print "<table border>";
print "<tr><th>" . _("ID") . "</th><th>" . _("Name") . "</th><th>" . _("Unique auto-id") . "</th><th>" . _("Last auto-update") . "</th><th>" . _("Uptime") . "</th><th>" . _("Owner") . "</th></tr>";

   while ($arr = mysql_fetch_array($query)) {
        # Try to suppress irritation from automach duplicates
        if ($prevkey <> $arr[f_key]) {
            print "<tr><td>" . $arr[f_key];
            print "<td><a href=\"$counter_site/cgi-bin/runscript/machine-show.cgi?machine=$arr[f_key]";
            maybecookie("&");
            print "\">";
            if ($arr[name]) {
               print $arr[name];
            } else {
               print _("Nameless");
            }
            print "</a>";
            $prevkey = $arr[f_key];
        } else {
           print "<tr><td><td>";
       }
        print "<td>";
        if ($arr[uniqueid]) {
           print "$arr[uniqueid]";
        }
        print "<td>";
        if ($arr[updatetime]) {
            print strftime("%Y-%m-%d %H:%M:%S", $arr[updatetime]);
        } else {
            print "<a href=\"$counter_site/scripts/\">No auto</a>";
        }
        if ($arr[uptime]) {
            printf("<td>%.1f", $arr[uptime]);
        } else {
            print "<td>";
        }
        if ($arr[owner]) {
            print "<td>" . $arr[owner];
        }
        print "\n";
       }

}
?>
</table>

<?php pagebottom() ?>
</body>
</html>
